package com.loongtech.bi.action.player;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.player.AlivePlayerManager;
import com.loongtech.bi.manager.player.AlivePlayerManager.AlivePlayerData;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 玩家分析-留存分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class AlivePlayerAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<AlivePlayerData> dataList;  // 留存数据
	private ChartUIData chartData;  // 留存图

    private String selectedType = "1";  // 1=新增设备 2=新增帐号 3=活跃帐号 4=首付帐号

    private Date schoolDate = new Date(Time.getBeginningOfDay(Time.currentTime() - 2 * Time.DAY));
    private List<AlivePlayerManager.RoleSchoolLerfData> dataSchoolList;  // 留存数据
    private ChartUIData chartSchoolData;  // 留存图
    private String queryPlatform2 = "0"; // 前端传过来的查询条件：平台
    private String queryChannel2 = "0"; // 前端传过来的查询条件：渠道
    private String queryServer2 = "0"; // 前端传过来的查询条件：区服
    private Map<String, String> platformItmes2; // 平台下拉列表
    private Map<String, String> channelItmes2; // 渠道下拉列表
    private Map<String, String> serverItmes2; // 区服下拉列表

	@Autowired
	AlivePlayerManager alivePlayerManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
        initData2();

		search();
        search2();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "查询玩家分析-留存分析");

        selectType();
	}

    public void search2() {
        sysLogManager.addLog(session.getUserName(), "查询玩家分析-留存分析");

        dataSchoolList = alivePlayerManager.getAllRoleSchoolData(schoolDate, queryPlatform2, queryChannel2, queryServer2);
        List<AlivePlayerManager.RoleSchoolLeftChartData> list = alivePlayerManager.getRoleSchoolleftChartData(dataSchoolList);
        if (list != null && list.size() > 0) {
            chartSchoolData = new ChartUIData();
            chartSchoolData.setJsonType(ChartType.SPLINE.type);
            chartSchoolData.setJsonStr(new ChartTool(list, AlivePlayerManager.RoleSchoolLeftChartData.class, "").loadData(chartSchoolData.getJsonType(), true));
        } else {
            chartSchoolData = null;
        }
    }

    public void initData2() {
        platformItmes2 = new LinkedHashMap<>();
        platformItmes2.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes2.put(p.getName(), p.getId());
        }
        updateChannelItmes2();
    }

    public void updateChannelItmes2() {
        if (!"0".equals(queryPlatform2)) {
            channelItmes2 = new LinkedHashMap<>();
            channelItmes2.put("全渠道", "0");
            serverItmes2 = new LinkedHashMap<>();
            serverItmes2.put("全区服", "0");

            return;
        }

        List<EntityBiChannel> list = BiPlatformManager.getChannelIdsByPlatformId(queryPlatform2);

        channelItmes2 = new LinkedHashMap<>();
        channelItmes2.put("全渠道", "0");
        for (EntityBiChannel c : list) {
            channelItmes2.put(c.getName(), c.getId());
        }

        updateServerItmes2();
    }

    public void updateServerItmes2() {
        if (!"0".equals(queryChannel2)) {
            platformItmes2 = new LinkedHashMap<>();
            platformItmes2.put("全平台", "0");
            serverItmes2 = new LinkedHashMap<>();
            serverItmes2.put("全区服", "0");

            return;
        }

        platformItmes2 = new LinkedHashMap<>();
        platformItmes2.put("全平台", "0");
        for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
            platformItmes2.put(p.getName(), p.getId());
        }

        List<EntityBiServer> list = BiPlatformManager.SERVER_LIST;

        serverItmes2 = new LinkedHashMap<>();
        serverItmes2.put("全区服", "0");
        for (EntityBiServer s : list) {
            serverItmes2.put(s.getName(), s.getId());
        }
    }

    public void selectedServer2() {
        if (!"0".equals(queryServer2)) {
            platformItmes2 = new LinkedHashMap<>();
            platformItmes2.put("全平台", "0");
            channelItmes2 = new LinkedHashMap<>();
            channelItmes2.put("全渠道", "0");

            return;
        }
        initData2();
    }

	public void selectType() {
        dataList = new ArrayList<>();
        chartData = null;

        dataList = alivePlayerManager.getAllData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, selectedType);
        if (dataList != null && dataList.size() > 0) {
            chartData = new ChartUIData();
            chartData.setJsonType(ChartType.COLUMN.type);
            chartData.setJsonStr(new ChartTool(dataList, AlivePlayerData.class, "").loadData(chartData.getJsonType()));

            AlivePlayerData z = alivePlayerManager.new AlivePlayerData();
            z.setDateStr("平均数");
            z.setPlatformId(queryPlatform);
            z.setChannelId(queryChannel);
            z.setServerId(queryServer);
            int nu = 0;
            int t1 = 0;
            int t2 = 0;
            int t3 = 0;
            int t4 = 0;
            int t5 = 0;
            int t6 = 0;
            int t7 = 0;
            int t14 = 0;
            int t30 = 0;


            for (AlivePlayerData t : dataList) {
                if (t.getNewUser() > 0) {
                    nu++;
                    z.setNewUser(t.getNewUser() + z.getNewUser());
                }
                if (z.getThe2Left() > 0) {
                    t2++;
                    z.setThe2Left(t.getThe2Left() + z.getThe2Left());
                }
                if (z.getThe3Left() > 0) {
                    t3++;
                    z.setThe3Left(t.getThe3Left() + z.getThe3Left());
                }
                if (z.getThe4Left() > 0) {
                    t4++;
                    z.setThe4Left(t.getThe4Left() + z.getThe4Left());
                }
                if (z.getThe5Left() > 0) {
                    t5++;
                    z.setThe5Left(t.getThe5Left() + z.getThe5Left());
                }
                if (z.getThe6Left() > 0) {
                    t6++;
                    z.setThe6Left(t.getThe6Left() + z.getThe6Left());
                }
                if (z.getThe7Left() > 0) {
                    t7++;
                    z.setThe7Left(t.getThe7Left() + z.getThe7Left());
                }
                if (z.getThe14Left() > 0) {
                    t14++;
                    z.setThe14Left(t.getThe14Left() + z.getThe14Left());
                }
                if (z.getThe30Left() > 0) {
                    t30++;
                    z.setThe30Left(t.getThe30Left() + z.getThe30Left());
                }
            }
            if(nu > 0) {
                z.setNewUser(z.getNewUser() / nu);
            }
            if(t1 > 0) {
                z.setThe2Left(z.getThe2Left() / t1);
            }
            if(t2 > 0) {
                z.setThe2Left(z.getThe2Left() / t1);
            }
            if(t3 > 0) {
                z.setThe3Left(z.getThe3Left() / t3);
            }
            if(t4 > 0) {
                z.setThe4Left(z.getThe4Left() / t4);
            }
            if(t5 > 0) {
                z.setThe5Left(z.getThe5Left() / t5);
            }
            if(t6 > 0) {
                z.setThe6Left(z.getThe6Left() / t6);
            }
            if(t7 > 0) {
                z.setThe7Left(z.getThe7Left() / t7);
            }
            if(t14 > 0) {
                z.setThe14Left(z.getThe14Left() / t14);
            }
            if(t30 > 0) {
                z.setThe30Left(z.getThe30Left() / t30);
            }
            dataList.add(0, z);
        }
    }
	
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
    protected String csvFileName() {
	    return "留存分析";
    }

	public List<AlivePlayerData> getDataList() {
		return dataList;
	}

	public void setDataList(List<AlivePlayerData> dataList) {
		this.dataList = dataList;
	}

    public ChartUIData getChartData() {
        return chartData;
    }

    public void setChartData(ChartUIData chartData) {
        this.chartData = chartData;
    }

    public String getSelectedType() {
        return selectedType;
    }

    public void setSelectedType(String selectedType) {
        this.selectedType = selectedType;
    }

    public Date getSchoolDate() {
        return schoolDate;
    }

    public void setSchoolDate(Date schoolDate) {
        this.schoolDate = schoolDate;
    }

    public List<AlivePlayerManager.RoleSchoolLerfData> getDataSchoolList() {
        return dataSchoolList;
    }

    public void setDataSchoolList(List<AlivePlayerManager.RoleSchoolLerfData> dataSchoolList) {
        this.dataSchoolList = dataSchoolList;
    }

    public ChartUIData getChartSchoolData() {
        return chartSchoolData;
    }

    public void setChartSchoolData(ChartUIData chartSchoolData) {
        this.chartSchoolData = chartSchoolData;
    }

    public String getQueryPlatform2() {
        return queryPlatform2;
    }

    public void setQueryPlatform2(String queryPlatform2) {
        this.queryPlatform2 = queryPlatform2;
    }

    public String getQueryChannel2() {
        return queryChannel2;
    }

    public void setQueryChannel2(String queryChannel2) {
        this.queryChannel2 = queryChannel2;
    }

    public String getQueryServer2() {
        return queryServer2;
    }

    public void setQueryServer2(String queryServer2) {
        this.queryServer2 = queryServer2;
    }

    public Map<String, String> getPlatformItmes2() {
        return platformItmes2;
    }

    public void setPlatformItmes2(Map<String, String> platformItmes2) {
        this.platformItmes2 = platformItmes2;
    }

    public Map<String, String> getChannelItmes2() {
        return channelItmes2;
    }

    public void setChannelItmes2(Map<String, String> channelItmes2) {
        this.channelItmes2 = channelItmes2;
    }

    public Map<String, String> getServerItmes2() {
        return serverItmes2;
    }

    public void setServerItmes2(Map<String, String> serverItmes2) {
        this.serverItmes2 = serverItmes2;
    }
}
